package com.hotel.user.service;


import java.util.List;
import java.util.UUID;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.hotel.user.mapper.UserMapper;
import com.hotel.user.pojo.User;
import com.jt.common.utils.MapperUtil;

import redis.clients.jedis.JedisCluster;

@Service
public class UserService {
	
	@Autowired
	private JedisCluster jedis;
	
	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private RestTemplate restTemplate;
	//校验用户名是否可用
	public Boolean checkUserName(String userName) {
		int result=userMapper.selectUserCountByUserName(userName);
		//result不是1就是0
		return result==1;
	}
	//注册
	public void saveUser(User user) {
		//封装userId
		String userId=UUID.randomUUID().toString();
		user.setId(userId);
		//加密password
		String md5Password=DigestUtils.md5Hex(user.getPassword());
		System.out.println("密码密文:"+md5Password);
		user.setPassword(md5Password);
		
		userMapper.insertUser(user);
	}
	
	//登录
	public String doLogin(User user) {
		//依然要进行user校验工作
		user.setPassword(DigestUtils.md5Hex
				(user.getPassword()));
		User exist=userMapper
				.selectUserByUserNameAndPassword(user);
		if(exist==null){//登录失败
			return "";
		}else{//要引入顶替的逻辑
			//准备2个key值
			try{                
			String userLoginIdent=
					"user_login_"+exist.getId();
			String ticket="EM_TICKET"+
					System.currentTimeMillis()+exist.getId();
			String userJson=
					MapperUtil.MP.writeValueAsString(exist);
				if(jedis.exists(userLoginIdent)){
					//删除上次登录的ticket并且删除
					jedis.del(jedis.get(userLoginIdent));
				}
				//设置登录顶替相关的2个key-数据value
				jedis.setex(userLoginIdent, 60*60*3, ticket);
				jedis.setex(ticket, 60*60*2, userJson);
				return ticket;
			}catch(Exception e){
				e.printStackTrace();
				return "";
			}
		}
	}
	//超时续约
	public String checkLoginUserData(String ticket) {
		String userLoginIdent="user_login_"+ticket.substring(22);
		//加入剩余时间续约的判断
		//判断剩余时间
		Long leftTime = jedis.pttl(ticket);
		//判断是否小于30分钟 1000*60*30
		if(leftTime<1000*60*30){
			//进行续约,将超时时间加长1小时
			Long expireTime=leftTime+1000*60*60;
			jedis.pexpire(ticket, expireTime);
			//对userLoginIdent做续约保证
			//userId才能对userLoginIdent 对ticket值作截取 操作
			jedis.pexpire(userLoginIdent,
			jedis.pttl(userLoginIdent)+1000*60*60);
		}
		return jedis.get(ticket);
	}
	
	/**
	 * 后台用户系统
	 */
	//根据id查找用户
	public User queryUser(String id) {
		return userMapper.selectUsers(id);
	}
	//添加用户
	public void addUser(User user) {
		String id=UUID.randomUUID().toString();
		user.setId(id);
		//加密password
		String md5Password=DigestUtils.md5Hex(user.getPassword());
		System.out.println("密码密文:"+md5Password);
		user.setPassword(md5Password);
		userMapper.insertUser(user);
	}

	//删除用户
	public void deleteUser(String id) {
		userMapper.deleteUser(id);
		//根据用户id删除订单
		String url="http://orderservice/order/manage/deleteOrderByUserId?userId="+id;
		String result=restTemplate.getForObject(url, String.class);

	}

	//修改用户信息
	public void updateUser(User user) {
		String md5Password=DigestUtils.md5Hex(user.getPassword());
		System.out.println("密码密文:"+md5Password);
		user.setPassword(md5Password);
		userMapper.updateUser(user);
		
	}

	//查询全部用户信息
	public List<User> queryUsers() {
	 return userMapper.selectUser();
	}
	
	//order调用
	public User findUserByUserName(String userName) {
		return userMapper.selectUserByUserName(userName);
	}
}



